<?php
class Mmodproduct_site_product extends CI_Model{

    private $_table = "site_product";
    
    public function __construct(){
        parent::__construct();
        $this->load->database();
    }

    function getRandomData($record_number, $record_start){
        $this->db->select('n.*, m.Name as MName');
        $this->db->from($this->_table." n");
        $this->db->where("n.Status", 1);
        $this->db->join('site_product_menu m', 'n.Menu = m.ID');
        $this->db->limit($record_number, $record_start);
        $this->db->order_by("rand()");
        $query = $this->db->get();
        $data = $query->result_array();
        return $data;
    }

    function getGroupMenu(){
        $this->db->select('n.*, m.Name as MName');
        $this->db->from($this->_table." n");
        $this->db->where("n.Status", 1);
        $this->db->join('site_product_menu m', 'n.Menu = m.ID');
        $this->db->group_by("n.Menu");
        $this->db->order_by("n.Menu","ASC");
        $query = $this->db->get();
        $data = $query->result_array();
        return $data;
    }

    function getHotProduct($record_number, $record_start){
        $this->db->select('n.*, m.Name as MName');
        $this->db->from($this->_table." n");
        $this->db->where("n.Hot", 1);
        $this->db->where("n.Status", 1);
        $this->db->join('site_product_menu m', 'n.Menu = m.ID');
        $this->db->limit($record_number, $record_start);
        $this->db->order_by("n.Dateset","DESC");
        $this->db->order_by("n.ID","DESC");
        $query = $this->db->get();
        $data = $query->result_array();
        return $data;
    }

    function getAllData($record_number, $record_start){
        $this->db->select('n.*, m.Name as MName');
        $this->db->from($this->_table." n");
        $this->db->join('site_product_menu m', 'n.Menu = m.ID');
        $this->db->where("n.Status", 1);
        $this->db->limit($record_number, $record_start);
        $this->db->order_by("n.Dateset","DESC");
        $this->db->order_by("n.ID","DESC");
        $query = $this->db->get();
        $data = $query->result_array();
        return $data;
    }

    function countAllData(){
        $this->db->select('count(*) as Sum');
        $this->db->from($this->_table." n");
        $this->db->join('site_product_menu m', 'n.Menu = m.ID');
        $this->db->where("n.Status", 1);
        $query = $this->db->get();
        $data = $query->result_array();
        return $data[0]['Sum'];
    }
    
    function getDataByID($id){
        $this->db->select('n.*, m.ID as MID, m.Name as MName');
        $this->db->from($this->_table." n");
        $this->db->join('site_product_menu m', 'n.Menu = m.ID');
        $this->db->where("n.ID", $id);
        $this->db->where("n.Status", 1);
        $query = $this->db->get($this->_table);
        
        if($query)
            return $query->row_array();
        else
            return FALSE;
    }

    function getSearchData($name, $menu, $costf, $costt, $record_number, $record_start){
        $this->db->select('n.*');
        $this->db->from($this->_table." n");
        $this->db->where("n.Status = 1");
        if($name != "Tên sản phẩm" && $name != " "){
            $this->db->where("(n.Name like '%$name%' or n.Code like '%$name%')");;
        }
        if(is_numeric($menu)){
            $this->db->where("(n.Menu = $menu or n.Menu in (SELECT ID FROM site_product_menu WHERE PID = $menu))");
        }
        if(is_numeric($costf) && $costf > 1){
            $this->db->where("n.Cost >= $costf");
        }
        if(is_numeric($costt) && $costt > 0){
            $this->db->where("n.Cost <= $costt");
        }
        $this->db->limit($record_number, $record_start);
        $this->db->order_by("n.Dateset","DESC");
        $this->db->order_by("n.ID","DESC");
        $query = $this->db->get();
        $data = $query->result_array();
        return $data;
    }

    function countSearchData($name, $menu, $costf, $costt){
        $this->db->select('count(*) as Sum');
        $this->db->from($this->_table." n");
        $this->db->where("n.Status = 1");
        if($name != "Tên sản phẩm" && $name != " "){
            $this->db->where("(n.Name like '%$name%' or n.Code like '%$name%')");;
        }
        if(is_numeric($menu)){
            $this->db->where("(n.Menu = $menu or n.Menu in (SELECT ID FROM site_product_menu WHERE PID = $menu))");
        }
        if(is_numeric($costf) && $costf > 0){
            $this->db->where("n.Cost >= $costf");
        }
        if(is_numeric($costt) && $costt > 0){
            $this->db->where("n.Cost <= $costt");
        }
        $query = $this->db->get();
        $data = $query->result_array();
        return $data[0]['Sum'];
    }

    function getTagsData($name, $record_number, $record_start){
        $this->db->select('n.*');
        $this->db->from($this->_table." n");
        $this->db->where("n.Status = 1");
        $this->db->where("n.Tags like '%$name%'");
        $this->db->limit($record_number, $record_start);
        $this->db->order_by("n.Dateset","DESC");
        $this->db->order_by("n.ID","DESC");
        $query = $this->db->get();
        $data = $query->result_array();
        return $data;
    }

    function countTagsData($name){
        $this->db->select('count(*) as Sum');
        $this->db->from($this->_table." n");
        $this->db->where("n.Status = 1");
        $this->db->where("n.Tags like '%$name%'");
        $query = $this->db->get();
        $data = $query->result_array();
        return $data[0]['Sum'];
    }

    function getMenuData($menu, $record_number, $record_start){
        $this->db->select('n.*, m.Name as MName');
        $this->db->from($this->_table." n");
        $this->db->join('site_product_menu m', 'n.Menu = m.ID');
        $this->db->where("n.Status", 1);
        $this->db->where("(n.Menu = $menu or n.Menu in (SELECT ID FROM site_product_menu WHERE PID = $menu))");
        $this->db->limit($record_number, $record_start);
        $this->db->order_by("n.Dateset","DESC");
        $this->db->order_by("n.ID","DESC");
        $query = $this->db->get();
        $data = $query->result_array();
        return $data;
    }

    function countMenuData($menu){
        $this->db->select('count(*) as Sum');
        $this->db->from($this->_table." n");
        $this->db->join('site_product_menu m', 'n.Menu = m.ID');
        $this->db->where("n.Status", 1);
        $this->db->where("(n.Menu = $menu or n.Menu in (SELECT ID FROM site_product_menu WHERE PID = $menu))");
        $query = $this->db->get();
        $data = $query->result_array();
        return $data[0]['Sum'];
    }

    function addHit($id){
        $this->db->query("update ".$this->_table." set Hit = Hit+1 where ID = '$id'");
        return TRUE;
    }

    function getSimilarData($menu, $id, $record_number, $record_start){
        $this->db->select('n.*, m.Name as MName');
        $this->db->from($this->_table." n");
        $this->db->join('site_product_menu m', 'n.Menu = m.ID');
        $this->db->where("n.Status", 1);
        $this->db->where("n.Menu", $menu);
        $this->db->where("n.ID !=", $id);
        $this->db->limit($record_number, $record_start);
        $this->db->order_by("n.Dateset","DESC");
        $this->db->order_by("n.ID","DESC");
        $query = $this->db->get();
        $data = $query->result_array();
        return $data;
    }
}
?>
